package com.quanyan.club.mq.producer.impl;

import com.google.gson.Gson;
import com.quanyan.club.mq.producer.ClubMedalProducer;
import com.quanyan.mq.client.QcMsgClient;
import com.quanyan.mq.client.reqeust.MqRequestMsg;
import com.quanyan.mq.client.reqeust.SendCallbackListener;
import com.quanyan.mq.client.response.MqResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;

/**
 * Created by zhang on 16/8/10.
 */
@Component("clubMedalProducerImpl")
public class ClubMedalProducerImpl implements QcMsgClient {

    private final static Logger logger = LoggerFactory.getLogger(ClubMedalProducerImpl.class);

    @Autowired
    private ClubMedalProducer clubMedalProducer;

    @Nonnull
    @Override
    public MqResponse syncSend(@Nonnull MqRequestMsg mqRequestMsg) {
        MqResponse mqResponse = this.clubMedalProducer.syncSend(mqRequestMsg);
        return mqResponse;
    }

    @Override
    public void asyncSend(@Nonnull MqRequestMsg mqRequestMsg) {
        logger.warn("SEND TO MQ : DATA : {}", new Gson().toJson(mqRequestMsg).toString());
        clubMedalProducer.asyncSend(mqRequestMsg, new SendCallbackListener() {
            @Override
            public void onSuccess(String sendResult) {
                logger.warn("SEND TO MQ : RESULT : {}", sendResult);
            }

            @Override
            public void onException(Throwable e) {
                logger.warn("SEND TO MQ EXCEPTION : {}", e.getMessage());
            }
        });
    }
}
